<template>
	<!-- 车辆编辑 -->
	<div>
		<div class="edit-top">
			车辆信息
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-title">车辆信息</div>
			<div class="edit-item">
			   <label>车架号</label><input v-model="info.vin" disabled="disabled" type="text" />
			 </div>
			 <div class="edit-item">
			   <label>型号</label><input v-model="info.model" disabled="disabled" type="text" />
			 </div>
			 <div class="edit-item">
			   <label>发动机号</label><input v-model="info.engineCode" disabled="disabled" type="text" />
			 </div>
			 <div class="edit-item">
			 	<label>车辆类型</label><select v-model="info.vehicleType" disabled="disabled">
					<option v-for='item in dict.vehicleType' :value="item.value">{{item.type}}</option>
			 	</select>
			 </div>
			 <div class="edit-item">
			   <label>生产厂家</label><input v-model="info.manufacturer" disabled="disabled" type="text" />
			 </div>
			 <div class="edit-item">
			   <label>随车电池包号</label><input v-model="info.initBatteryCode" type="text"  disabled="disabled" />
			 </div>
			<div class="edit-item">
				<label>行驶证</label><input v-model="new_info.drivingLicenseNum" :disabled="flag == 1" type="text" />
			</div>
			<div class="edit-item">
				<label>车牌号</label><input v-model="new_info.licensePlateNumber" :disabled="flag == 1" type="text" />
			</div>
			<div class="edit-item">
				<label>车牌类型</label><select v-model="new_info.licensePlateType" :disabled="flag == 1">
					<option v-for='item in dict.licensePlateType' :value="item.value">{{item.type}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>产权人类型</label><select v-model="new_info.ownerType" :disabled="flag == 1">
					<option v-for='item in dict.ownerType' :value="item.value">{{item.type}}</option>
				</select>
			</div>
			<template v-if="flag == 1">
				<div class="edit-item">
				  <label>车主姓名</label><input v-model="info.ownerName" type="text" :disabled="flag == 1" />
				</div>
				<div class="edit-item">
				   <label>车主身份证</label><input v-model="info.ownerIdcard" type="text" :disabled="flag == 1" />
				 </div>
				 <div class="edit-item">
				   <label>车主手机号</label><input v-model="info.ownerPhoneNum" type="text" :disabled="flag == 1" />
				 </div>
				 <div class="edit-item">
				   <label>联系人手机</label><input v-model="info.contactsPhoneNum" type="text" :disabled="flag == 1" />
				 </div>
			</template>
			<template v-if="flag != 1">
				<div class="edit-item">
				  <label>车主姓名</label><select v-model="owner" @change="owner_change" :disabled="flag == 1">
						<option v-for='item in client' :value="item">{{item.chineseName}}</option>
					</select>
				</div>
				<div class="edit-item">
				   <label>车主身份证</label><input v-model="new_info.ownerIdcard" type="text" :disabled="flag == 1" />
				 </div>
				 <div class="edit-item">
				   <label>车主手机号</label><input v-model="new_info.ownerPhoneNum" type="text" :disabled="flag == 1" />
				 </div>
				 <div class="edit-item">
				   <label>联系人手机</label><input v-model="new_info.contactsPhoneNum" type="text" :disabled="flag == 1" />
				 </div>
			</template>
			
			<div class="edit-item">
				<label>使用性质</label><select v-model="new_info.property" :disabled="flag == 1">
					<option v-for='item in dict.property' :value="item.value">{{item.type}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>产权证书号</label><input v-model="new_info.propertyRightCode" :disabled="flag == 1" type="text" />
			</div>
		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' v-if="flag != 1" type="primary">保存</el-button>
			<el-button @click='history' v-if="info.status == 5" type="primary">交班记录</el-button>
			<el-button @click='history_gps'  type="primary">实时轨迹</el-button>
		</div>
	</div>
</template>

<script>
	import Global from '@/components/Global.vue'
	import eventBus from '@/assets/js/eventBus'
	export default {
		props: ['id', 'status','flag'],

		data() {
			return {
				dict: '',
				client:[],
				processId:'',
				new_info: {
					id: '',
					drivingLicenseNum: '',
					licensePlateNumber:'',
					licensePlateType: 0,
					ownerType:0,
					property: 0,
					propertyRightCode: '',
					ownerIdcard:'',
					ownerName:'',
					ownerPhoneNum:'',
					contactsId:'',
					contactsPhoneNum:'',
				},
				owner:{},
				info: {
					
				}

			}
		},
		mounted() {
			this.get_dict();
			this.get_info();
			if(this.flag != 1){
				this.get_client();
			}
		},
		methods: {
			owner_change(){
				this.new_info.ownerIdcard = this.owner.identityNum;
				this.new_info.ownerPhoneNum = this.owner.mobileNum;
				this.new_info.ownerName = this.owner.chineseName;
				this.new_info.contactsId = this.owner.id;
				this.new_info.contactsPhoneNum = this.owner.mobileNum;
			},
			history_gps(){
				this.$emit("saveEdit", 4);
			},
			history(){
				this.$emit("saveEdit", 3);
			},
			get_client(){
				var self = this;
				this.$axios.post(this.$global.serverIp + 'admin/sysClient/findPage', {
				"limit": "10000",
				"page": "1"
				  })
				  .then(function (res) {
					  if(res != ''){
						 self.client = res.data.list;
					  }
						
				  })
			},
			get_dict() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=vehicle')
				  .then(function (res) {
					  if(res != '')
						self.dict = res.data;
				  })
			},
			get_info() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'vehicle/vehicle/info/' + this.id)
					.then(function(res) {
						if (res != ''){
							self.info = res.data.sysflow.object;
							self.new_info.id = self.info.id;
							
								self.new_info.drivingLicenseNum = self.info.drivingLicenseNum;
								self.new_info.licensePlateNumber = self.info.licensePlateNumber;
								self.new_info.licensePlateType = self.info.licensePlateType == null ? 0 : self.info.licensePlateType;
								self.new_info.ownerType = self.info.ownerType == null ? 0 : self.info.ownerType;
								self.new_info.property = self.info.property == null ? 0 : self.info.property;
								self.new_info.propertyRightCode = self.info.propertyRightCode;
								self.owner = self.info;
								
							
							
							self.processId = res.data.processId;
						}
					})
					
				
			},
			save() {
				if (this.new_info.licensePlateNumber == null) {
					this.$popup.open('车牌号不能为空');
					return;
				}
				if (this.new_info.drivingLicenseNum == null) {
					this.$popup.open('行驶证不能为空');
					return;
				}
				if (this.new_info.propertyRightCode == null) {
					this.$popup.open('产权证书号不能为空');
					return;
				}
				if (this.new_info.ownerName == null) {
					this.$popup.open('车主姓名不能为空');
					return;
				}
				if (this.new_info.ownerIdcard == null) {
					this.$popup.open('车主身份证不能为空');
					return;
				}
				if (this.new_info.ownerPhoneNum == null) {
					this.$popup.open('车主手机号不能为空');
					return;
				}
				var self = this;
				this.new_info.processId = self.processId;
				this.new_info.id = self.id;
				this.new_info.status = status;
				this.$axios.post(this.$global.serverIp + 'vehicle/vehicle/startUsing', self.new_info)
				  .then(function (res) {
					  if(res != ''){
						 self.success('编辑成功');
					  }
						
				  })
			},
			success(content) {
				var self = this;
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						self.$emit("saveEdit", 2);
					}
				});
			},
			
			cancel() {
				this.$emit("saveEdit", 1);
			}
		}
	}
	import '@/assets/css/edit.css'
</script>
